package com.cm.sys.system.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.AlarmMessage;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.service.MessageService;

/***
 * 站内信管理
 * 
 * @author yxq18
 * @date 2020/01/06
 */
@RequestMapping(value = "/message")
@Controller
public class MessageController {
    private Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    /***
     * 消息列表
     * 
     * @return
     */
    @RequestMapping(value = "/messageList", method = RequestMethod.POST)
    public ResponseResult getMessageList(RequestParameter rp) {

        ResponseResult rr = new ResponseResult();

        JSONObject args = rp.getparameters();
        logger.info("消息列表的入参============【{}】==============", args);
        Integer pageIndex = JsonUtil.getInt(args, "pageIndex", 0);

        Integer pageSize = JsonUtil.getInt(args, "pageSize", 10);
        User user = JwtUtils.validateToken(rp.getToken());
        Map<String, Object> map = messageService.selectMessageList(pageIndex, pageSize, user.getId());

        rr.setMap(map);

        return rr;

    }

    /***
     * 未读消息数量
     * 
     * @return
     */
    @RequestMapping(value = "/messageCount", method = RequestMethod.POST)
    public ResponseResult getUnReadMessageCount(RequestParameter rp) {

        ResponseResult rr = new ResponseResult();

        User user = JwtUtils.validateToken(rp.getToken());
        Integer count = messageService.getUnReadMessageCount(user.getId());
        Map<String, Object> map = new ConcurrentHashMap<>(16);
        map.put("count", count);
        rr.setMap(map);
        return rr;
    }

    /***
     * 消息详情
     * 
     * @return
     */
    @RequestMapping(value = "/messageInfo", method = RequestMethod.POST)
    public ResponseResult getMessageInfo(RequestParameter rp) {

        ResponseResult rr = new ResponseResult();

        JSONObject args = rp.getparameters();
        logger.info("消息详情的入参============【{}】==============", args);
        Integer messageId = JsonUtil.getInt(args, "messageId");
        User user = JwtUtils.validateToken(rp.getToken());
        // 先保存在查看详情 判断数据库是否存在这数据
        if (StringUtils.isEmpty(messageService.getMessage(messageId, user.getId()))) {
            messageService.saveMessage(messageId, user.getId(), 1); // 0表示未读 1表示已读
        }

        AlarmMessage alarmMessage = messageService.getMessageInfo(messageId);
        rr.setObject(alarmMessage);
        return rr;

    }

}
